package com.xbongbong.pro.statistic.help.strategy.chart.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.service.toolbox.statistic.help.ChartDtoHelp;
import com.xbongbong.pro.statistic.enums.ChartTypeEnum;
import com.xbongbong.pro.statistic.enums.StatisticsTypeEnum;
import com.xbongbong.pro.statistic.help.strategy.chart.ChartStrategyService;
import com.xbongbong.pro.statistic.model.ChartCustomModel;
import com.xbongbong.pro.statistic.model.ChartModel;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartResultBaseDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartResultDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 自定义和系统图表都返回
 * @author xinpeng.jiang
 * @version V1.0
 * @date 2021/5/17 10:00
 * @since V1.0
 */
@Service
public class AllChartStrategyServiceImpl implements ChartStrategyService {
    @Resource
    private ChartCustomModel chartCustomModel;
    @Resource
    private ChartModel chartModel;
    @Resource
    private ChartDtoHelp chartDtoHelp;

    @Override
    public ChartEntity getByKey(Long chartId, String corpid) {
        return null;
    }

    @Override
    public List<ChartEntity> getChartList(ChartResultBaseDTO chartResultBaseDTO) throws XbbException {
        //处理chartIdIn，把系统图表和自定义图表分装到sysIdIn、customIdIn
        chartDtoHelp.dealWithIdIn(chartResultBaseDTO);
        List<ChartEntity> list = new ArrayList<>();
        //塞入map
        Map<String, Object> map = BeanUtil.convertBean2Map(chartResultBaseDTO, true);
        map.put("del", DelEnum.NORMAL.getDel());
        if (CollectionsUtil.isNotEmpty(chartResultBaseDTO.getSysIdIn())) {
            // 系统
            map.put("statisticsType", StatisticsTypeEnum.SYSTEM_STATISTIC.getCode());
            //系统的使用的 chartResultDTO.getSysIdIn()
            map.put("idIn", chartResultBaseDTO.getSysIdIn());
            list.addAll(chartModel.findEntitys(map));
        }
        if (CollectionsUtil.isNotEmpty(chartResultBaseDTO.getCustomIdIn())) {
            // 自定义
            map.put("statisticsType", StatisticsTypeEnum.CUSTOM_STATISTIC.getCode());
            //自定义用chartResultDTO.getCustomIdIn()
            map.put("idIn", chartResultBaseDTO.getCustomIdIn());
            list.addAll(chartCustomModel.findEntitys(map));
        }
        return list;
    }

    @Override
    public List<ChartEntity> getChartList(Map<String, Object> param) throws XbbException {

        param.put("del", DelEnum.NORMAL.getDel());
        List<ChartEntity> list = new ArrayList<>(chartModel.findEntitys(param));
        list.addAll(chartCustomModel.findEntitys(param));
        return list;
    }

    @Override
    public List<ChartEntity> getPkChartList(ChartResultDTO chartResultDTO) throws XbbException {
        //处理chartIdIn，把系统图表和自定义图表分装到sysIdIn、customIdIn
        chartDtoHelp.dealWithIdIn(chartResultDTO);
        //查找图表列表
        Map<String, Object> map = BeanUtil.convertBean2Map(chartResultDTO, true);
        map.put("del", DelEnum.NORMAL.getDel());
        List<ChartEntity> list = new ArrayList<>();
        if (CollectionsUtil.isNotEmpty(chartResultDTO.getSysIdIn())) {
            // 系统
            map.put("chartType", ChartTypeEnum.PK.getCode());
            map.put("statisticsType", StatisticsTypeEnum.SYSTEM_STATISTIC.getCode());
            //系统的使用的 chartResultDTO.getSysIdIn()
            map.put("idIn", chartResultDTO.getSysIdIn());
            list.addAll(chartModel.findEntitys(map));
        }
        if (CollectionsUtil.isNotEmpty(chartResultDTO.getCustomIdIn())) {
            // 自定义
            map.put("chartType", ChartTypeEnum.PK_CUSTOM.getCode());
            map.put("statisticsType", StatisticsTypeEnum.CUSTOM_STATISTIC.getCode());
            //系统的使用的 chartResultDTO.getIdIn()，自定义用chartResultDTO.getCustomIdIn()
            map.put("idIn", chartResultDTO.getCustomIdIn());
            list.addAll(chartCustomModel.findEntitys(map));
        }
        return list;
    }

    /**
     * 这个方法废弃吧
     * @author xinpeng.jiang
     * @date 2021/5/19 14:34
     * @version 1.0
     * @since V1.0
     */
    @Override
    public Integer updateChart(ChartEntity entity) throws XbbException {
        return null;
    }

    /**
     * 批量保存数值格式
     * @param chartResultDTO
     * @author xinpeng.jiang
     * @date 2021/5/19 14:34
     * @version 1.0
     * @since V1.0
     */
    @Override
    public void batchUpdateNumFormat(ChartResultDTO chartResultDTO) throws XbbException {
        //处理chartIdIn，把系统图表和自定义图表分装到sysIdIn、customIdIn
        chartDtoHelp.dealWithIdIn(chartResultDTO);
        //保存
        if (CollectionsUtil.isNotEmpty(chartResultDTO.getSysIdIn())) {
            // 系统
            chartModel.batchUpdateNumFormat(chartResultDTO.getCorpid(), chartResultDTO.getSysIdIn(), chartResultDTO.getNumFormat());
        }
        if (CollectionsUtil.isNotEmpty(chartResultDTO.getCustomIdIn())) {
            // 自定义
            chartCustomModel.batchUpdateNumFormat(chartResultDTO.getCorpid(), chartResultDTO.getCustomIdIn(), chartResultDTO.getNumFormat());
        }
    }
}
